package com.jianggujin.redis;

import java.awt.Desktop;
import java.net.URI;
import java.util.List;

import javax.swing.tree.DefaultMutableTreeNode;

import com.jianggujin.redis.component.ConnectionTree.ConnectionListener;
import com.jianggujin.redis.component.MainMenuBar.MainMenuListener;
import com.jianggujin.redis.config.ConnectionConfig;
import com.jianggujin.redis.dialog.ConnectionsDialog;
import com.jianggujin.redis.dialog.RedisClientsMonitorDialog;
import com.jianggujin.redis.dialog.RedisInfoDialog;
import com.jianggujin.redis.dialog.RedisPropertyDialog;
import com.jianggujin.redis.node.RedisConnection;
import com.jianggujin.redis.node.RedisDatabase;
import com.jianggujin.redis.node.RedisKey;
import com.jianggujin.redis.node.RedisKeys;
import com.jianggujin.redis.util.MessageUtils;

public class MainFrameHandler implements MainMenuListener, ConnectionListener {

    private MainFrame frame;

    public MainFrameHandler(MainFrame frame) {
        this.frame = frame;
    }

    /********************* 主菜单 ********************/
    @Override
    public void handleOpenConnect() {
        ConnectionConfig config = ConnectionsDialog.connect(frame);
        if (config == null) {
            return;
        }
        this.frame.getConnectionTree().connect(config);
    }

    @Override
    public void handleShowAbout() {
        MessageUtils.showInfoMessage(frame, "About", "Author:蒋固金(Gene.Jiang)");
    }

    @Override
    public void handleReportBug() {
        Desktop desktop = Desktop.getDesktop();
        if (Desktop.isDesktopSupported() && desktop.isSupported(Desktop.Action.BROWSE)) {
            try {
                desktop.browse(new URI("https://gitee.com/jianggujin/RedisClient/issues"));
            } catch (Exception e) {
                MessageUtils.showErrorMessage(frame, "Open issues error!");
            }
        } else {
            MessageUtils.showInfoMessage(frame, "Browser not supported!");
        }
    }

    @Override
    public void handleQuitApplication() {
        if (MessageUtils.showConfirm(frame, "Are you sure quit application?")) {
            this.frame.getConnectionTree().disconnect();
            System.exit(0);
        }
    }

    /********************* 左侧树 ********************/

    @Override
    public void onConnectError(RedisConnection connection) {
        ConnectionConfig config = connection.getConfig();
        MessageUtils.showErrorMessage(frame, "Connect error:" + config.getHost() + ":" + config.getPort());
    }

    @Override
    public void handleSelectRedisKey(RedisKey redisKey) {
        this.frame.getKeyValuePanel().showValue(redisKey);
    }

    @Override
    public void onReloadTree() {
        this.frame.getCenterPane().setDividerLocation(this.frame.getCenterPane().getDividerLocation());
    }

    @Override
    public void onDeleteSomeKeys(RedisKeys redisKeys, List<DefaultMutableTreeNode> keys, List<RedisKey> deleteKeys) {
        MessageUtils.showWarningMessage(frame, "Some key delete fail");
    }

    @Override
    public void onDeleteKeys(RedisKeys redisKeys, List<DefaultMutableTreeNode> keys, List<RedisKey> deleteKeys) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onDeleteKeyError(RedisKey redisKey) {
        MessageUtils.showWarningMessage(frame, "Delete fail");
    }

    @Override
    public void onDeleteKeySuccess(RedisKey redisKey) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onResetKeysFilter(RedisDatabase database) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onFilterKeys(RedisDatabase database) {
        // TODO Auto-generated method stub

    }

    @Override
    public void handleFilterKeys(DefaultMutableTreeNode databaseNode) {
        RedisDatabase database = (RedisDatabase) databaseNode.getUserObject();
        RedisConnection client = database.getRedisConnection();
        String filter = MessageUtils.showInput(frame, "Filter Keys", "Filter Regex",
                database.hasFilter() ? database.getFilter() : client.getConfig().getKeysGlobStylePattern());
        this.frame.getConnectionTree().setFilterKeys(databaseNode, filter);
    }

    @Override
    public void onReloadKeys(RedisDatabase database) {
        // TODO Auto-generated method stub

    }

    @Override
    public void handleAddNewKey() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onRemove(RedisConnection connection) {
        // TODO Auto-generated method stub

    }

    @Override
    public void handleClientsMonitor(RedisConnection connection) {
        RedisClientsMonitorDialog.monitor(frame, connection);
    }

    @Override
    public void handleProperty(RedisConnection connection) {
        List<String> configs = connection.getClient().configGet("*");
        RedisPropertyDialog.showProperty(frame, configs);
    }

    @Override
    public void handleInfo(RedisConnection connection) {
        RedisInfoDialog.showInfo(frame, connection.getClient().info());
    }

    @Override
    public void onDisconnect(RedisConnection connection) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onConnectSuccess(RedisConnection connection) {
        // TODO Auto-generated method stub

    }

    @Override
    public void handleSetTTL(DefaultMutableTreeNode redisKeyNode, Long ttl) {
        String t = MessageUtils.showInput(frame, "Set TTL", "New TTL in seconds",
                ttl == null ? "-1" : String.valueOf(ttl));
        this.frame.getConnectionTree().setTTL(redisKeyNode, t);
    }
}
